package org.apache.lucene.analysis.compound.morphology;

import org.apache.lucene.analysis.util.CharArraySet;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
/**
 *
 * @author chandra
 *
 */
public class savarNa implements sandhi {
	
	public String normalizeITrans(String word) {
		String nw = "";
		for (int i = 0; i < word.length(); i++) {
			if (i > 0 && i < word.length()-1) {
			if (word.charAt(i) == 'A') {
				nw = nw + "aa";
			}
			else if (word.charAt(i) == 'I') {
				nw = nw + "ii";
			}
				else
					nw = nw + word.substring(i, i+1);
			}
			else
				nw = nw + word.substring(i, i+1);
		}
		return nw;
	}
	
	public boolean isInDict(CharArraySet dict, String word) {
		char [] warr = word.toCharArray();
		return dict.contains(warr, 0, word.length());
	}
	
	/* private boolean isInDict(CharArraySet dict, String word, int off, int len) {
		char[] arr = word.toCharArray();
		return dict.contains(arr, off, len);
	} */
	
	    /* public String split(char[] wordArrIn, int len, CharArraySet dict){
		String word="";
		for (int i = 0; i < len; i++)
			word += wordArrIn[i];
		word = normalizeITrans(word);
			
		word = word.toLowerCase();
		char[] wordArr = word.toCharArray();
		for(int i = 0; i < word.length()-2; i++){
			if(word.substring(i, i+2).equals("aa") || word.substring(i, i+2).equals("ii") ||
			   word.substring(i, i+2).equals("uu")) {
				String w1 = word.substring(0, i+1);
				String w2 = word.substring(i);
				String w3 = word.substring(i+1);
				String w4 = word.substring(i-1, i) + w3;
				if((dict.contains(wordArr, 0, i+1) == true && dict.contains(wordArr, i, word.length()-i) == true)) {
					String a = w1 + " " + w2;
					return a;
				}
				else if((dict.contains(wordArr, 0, i+1) == true && dict.contains(wordArr, i+1, word.length()-i-1) == true)) {
					String a = w1 + " " + w3;
					return a;
				}
				else if ((dict.contains(wordArr, 0, i+1) == true && dict.contains(w4.toCharArray(), 0, w4.length()) == true)) {
					String a = w1 + " " + w4;
					return a;
				}
			}//end if
			if (word.codePointAt(i) == 0x93E || word.codePointAt(i) == 0x942 ||
				word.codePointAt(i) == 0x940) {
				String w1 = word.substring(0, i); //raama
				String w2 = word.substring(i+1);
				if (word.codePointAt(i) == 0x93E){
					w2 = "आ" + w2;
				}
				else if (word.codePointAt(i) == 0x940) {
					w1 = w1 + "ि";
					w2 = "ई" + w2;
				}
				else if (word.codePointAt(i) == 0x942) {
					w1 = w1 + "ु";
					w2 = "उ" + w2;
				}
				if((dictionaryHas(dict, w1, 0, w1.length()) == true && dictionaryHas(dict, w2, 0, w2.length()) == true)) {
					String a = w1 + " " + w2;
					return a;
				}
				
			}
		}//end for
		
		//System.out.println("Word doesn't contain sandhi.");
		return null;
	}//end method
		 */
	public String split(char[] wordArrIn, int len, CharArraySet dict){
		String word="";
		for (int i = 0; i < len; i++)
			word += wordArrIn[i];
		word = normalizeITrans(word);
		
		word = word.toLowerCase();
		char[] wordArr = word.toCharArray();
		System.out.println("In savarna: " + word);
		for(int i = 0; i < word.length()-1; i++){
			if(word.substring(i, i+2).equals("aa") || word.substring(i, i+2).equals("ii") ||
			   word.substring(i, i + 2).equals("uu")) {
				String w1 = word.substring(0, i + 1);
				String w2 = word.substring(i);
				String w3 = word.substring(i + 1);
				String w4 = word.substring(i - 1, i) + w3;
				if ((isInDict(dict, w1) == true && isInDict(dict, w2) == true)) {
					String a = w1 + " " + w2;
					return a;
				} else if ((isInDict(dict, w1) == true && isInDict(dict, w3) == true)) {
					String a = w1 + " " + w3;
					return a;
				} else if ((isInDict(dict, w1) == true && isInDict(dict, w4) == true)) {
					String a = w1 + " " + w4;
					return a;
				}
			}// end if
			if (word.codePointAt(i) == 0x93E || word.codePointAt(i) == 0x942
				|| word.codePointAt(i) == 0x940) {
				String w1 = word.substring(0, i); // raama
				String w2 = word.substring(i + 1);
				String w3 = null;
				String w4 = null;
				String w5 = null;
				if (word.codePointAt(i) == 0x93E) {
					w3 = "आ" + w2;
					w4 = w2 + "ः";
					w2 = "अ" + w2;
					w5 = w1 + "ा";
				} else if (word.codePointAt(i) == 0x940) {
					w1 = w1 + "ि";
					w3 = "ई" + w2;
					w4 = w2 + "ः";
					w2 = "इ" + w2;
				} else if (word.codePointAt(i) == 0x942) {
					w1 = w1 + "ु";
					w3 = "ऊ" + w2;
					w4 = w2 + "ः";
					w2 = "उ" + w2;
				}
				String w6 = null;
				if (isInDict(dict, w1) == true)
					w6 = w1;
				else if (w5 != null && isInDict(dict, w5))
					w6 = w5;
				if (w6 != null) {
					if (isInDict(dict, w2) == true) {
						String a = w6 + " " + w2;
						return a;
					} else if (isInDict(dict, w3) == true) {
						String a = w6 + " " + w3;
						return a;
					} else if (isInDict(dict, w4) == true) {
						String a = w6 + " " + w3;
						return a;
					}
				}
			}// end if
		}// end for
		return null;
	}// end method
}//end class

